package com.lw.forward;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.lw.common.config.JSONFileReader;
import com.lw.common.utils.CallApiWrapperUtils;
import com.lw.common.utils.ResultVo;
import com.lw.common.utils.Utils;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * 这里是涟水，镇江项目api接口
 *  by lw on 2025年2月20日11:24:25
 */
@RestController
@RequestMapping("/for")
@Tag(name = "气象接口", description = "2-江苏气象接口")
public class ForwardApiController {

    @Resource
    JSONFileReader jsonFileReader;

    @Resource
    CallApiWrapperUtils callApiWrapper;

    //获取预报气象信息,-for 镇江项目
    @GetMapping("forecast/{method}/{fcstEle}")
    public ResultVo forecast(@PathVariable("method") String method,
                             @PathVariable("fcstEle") String fcstEle,
                             @RequestParam(value = "maxVT", defaultValue = "192") int maxVT) {
        JSONObject jsonObject = jsonFileReader.readJSONFile();
        if (method == null || (!jsonObject.containsKey(method))) {
            return ResultVo.error("抱歉，无权限");
        }
        JSONObject paramObj = jsonObject.getJSONObject(method);
        if (!paramObj.containsKey("time")) {
            return ResultVo.error("抱歉，无权限.请检查是否对应相关配置");
        }
        paramObj.put("time", Utils.getYyyyMmDd(Calendar.getInstance()));
        //jsonObject 转map
        paramObj.put("fcstEle", fcstEle);
        paramObj.put("maxVT", maxVT);
        HashMap<String, String> params = Utils.json2Map(paramObj);
        return callApiWrapper.callApiWrapper(method, params);
    }
    @GetMapping("forecast7D/{method}/{fcstEle}")
    public ResultVo forecast7D(@PathVariable("method") String method, @PathVariable("fcstEle") String fcstEle) {
        return forecast(method, fcstEle, 168);
    }
    @GetMapping("forecast15D/{method}/{fcstEle}")
    public ResultVo forecast(@PathVariable("method") String method, @PathVariable("fcstEle") String fcstEle) {
        return forecast(method, fcstEle, 384);
    }

    /**
     * 地面采集数据
     *
     * @param method 固定时间
     * @return
     */
    @GetMapping("index3/{method}")
    public ResultVo index3(@PathVariable("method") String method) {
        JSONObject jsonObject = jsonFileReader.readJSONFile();
        if (method == null || (!jsonObject.containsKey(method))) {
            return ResultVo.error("抱歉，无权限");
        }

        JSONObject paramObj = jsonObject.getJSONObject(method);
        //设置当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        String start = format + "000000";
        paramObj.put("time", start);
        //jsonObject 转map
        HashMap<String, String> params = Utils.json2Map(paramObj);

        return callApiWrapper.callApiWrapper(method, params);
    }

    @GetMapping("index4/{method}")
    public ResultVo index4(@PathVariable("method") String method) {
        JSONObject jsonObject = jsonFileReader.readJSONFile();
        if (method == null || (!jsonObject.containsKey(method))) {
            return ResultVo.error("抱歉，无权限");
        }

        JSONObject paramObj = jsonObject.getJSONObject(method);
        //设置当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        String start = format + "000000";
        paramObj.put("times", start);
        //jsonObject 转map
        HashMap<String, String> params = Utils.json2Map(paramObj);

        return callApiWrapper.callApiWrapper(method, params);
    }


    //获取气象信息,-for 涟水项目 -固定时间段
    @SaIgnore
    @GetMapping("index2/{method}")
    public ResultVo index2(@PathVariable("method") String method) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        String start = format + "000000";
        String end = format + "230000";
        JSONObject jsonObject = jsonFileReader.readJSONFile();
        if (method == null || (!jsonObject.containsKey(method))) {
            return ResultVo.error("抱歉，无权限");
        }
        //jsonObject 转map
        JSONObject paramObj = jsonObject.getJSONObject(method);

        //设置当前时间
        paramObj.put("timeRange", "[" + start + "," + end + "]");

        HashMap<String, String> params = Utils.json2Map(paramObj);

        return callApiWrapper.callApiWrapperNoEncrypt(method, params);
    }


}
